package controllers

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"os"
	"path"
	"quickConnect/models/globalValues"
	"quickConnect/models/utils"
)

type PathController struct {
	beego.Controller
}

func (c *PathController) Get() {
	key := c.GetString("key", "")
	if key != globalValues.Key {
	c.Ctx.ResponseWriter.WriteHeader(403)
		c.Ctx.WriteString("非法访问")
		c.StopRun()
	}
	aim := c.GetString("aim", "./")
	res, err := utils.GetPath(aim)
	if err != nil {
		fmt.Println(err)
	}
	c.Ctx.WriteString(res)
}

type MkdirController struct {
	beego.Controller
}

func (c *MkdirController) Get() {
	key := c.GetString("key", "")
	if key != globalValues.Key {
	c.Ctx.ResponseWriter.WriteHeader(403)
		c.Ctx.WriteString("非法访问")
		c.StopRun()
	}
	aim := c.GetString("dirname", "")
	aim=path.Join(globalValues.HomeDir,aim)
	if aim==""{
		c.Ctx.ResponseWriter.WriteHeader(403)
		c.Ctx.WriteString("未知错误")
		c.StopRun()
	}
	err:=os.MkdirAll(aim,os.ModePerm)
	if err != nil {
		fmt.Println(err)
	}
	c.Ctx.WriteString("success")
}
